from typing import Optional
from pydantic import BaseModel, conlist, conint, confloat
from settings import settings


class PilotTelemetry(BaseModel):
    base_station_current_power: list[float] = [0.0] * settings.SERVICES_MODULE_NUMBER

    pilots_power_rx: list[float] = [0.0] * 2  # rm
    pilots_power_rx_link_status: list[bool] = [False] * 2

    pilots_power_tx: list[float] = [0.0] * 2
    pilots_power_tx_link_status: list[bool] = [False] * 2

    sm_input_power: list[float] = [0.0] * settings.SERVICES_MODULE_NUMBER
    sm_output_power: list[float] = [0.0] * settings.SERVICES_MODULE_NUMBER

    sm_paps: list[float] = [0.0] * settings.SERVICES_MODULE_NUMBER

    pss_found: bool = False


class BSPower(BaseModel):
    service_bs_power = [0 for _ in range(settings.DMA_POWER_METERS_NUMBER)]
    service_cl_dl = [0 for _ in range(settings.DMA_POWER_METERS_NUMBER)]


class CFOAveraging(BaseModel):
    second_to_measure: conlist(int, min_items=0) = [10, 100, 500, 2000]


class CFOOffsets(BaseModel):
    lnk_rx: conlist(float, min_items=settings.SERVICES_RELAYS_NUMBER) = [0 for _ in
                                                                         range(settings.SERVICES_RELAYS_NUMBER)]
    lnk_tx: conlist(float, min_items=settings.SERVICES_RELAYS_NUMBER) = [0 for _ in
                                                                         range(settings.SERVICES_RELAYS_NUMBER)]
    service_rx: conlist(float, min_items=settings.DMA_POWER_METERS_NUMBER) = [0 for _ in
                                                                              range(settings.DMA_POWER_METERS_NUMBER)]
    service_tx: conlist(float, min_items=settings.DMA_POWER_METERS_NUMBER) = [0 for _ in
                                                                              range(settings.DMA_POWER_METERS_NUMBER)]

    class Config:
        validate_assignment = True


class OutputPowerLinkConfig(BaseModel):
    output_power: conint() = 23
    cable_loss: conint() = 0
    input_gain: confloat() = 0
    output_gain: confloat() = 0


class PilotInformation(BaseModel):
    rtr_pilot_input_power: conlist(float, min_items=settings.DMA_POWER_METERS_NUMBER) = [0.0] * settings.DMA_POWER_METERS_NUMBER
    rtr_pilot_output_power: conlist(float, min_items=settings.DMA_POWER_METERS_NUMBER) = [0.0] * settings.DMA_POWER_METERS_NUMBER
    rtrs_pilot_frequency: conlist(float, min_items=settings.SERVICES_RELAYS_NUMBER) = [0.0] * settings.SERVICES_RELAYS_NUMBER


class OutputPowerServiceConfig(BaseModel):
    calibrate: bool = True


class ServicesOutPower(BaseModel):
    service_out_power: conlist(float, min_items=0) = [39.0] * settings.DMA_POWER_METERS_NUMBER


class ComplexWaveform(BaseModel):
    t0: confloat() = 0
    dt: confloat() = 0
    data: list = []


class FFTSpectrum(BaseModel):
    f0: confloat() = 0
    df: confloat() = 0
    data: list = []


class AutomaticControlConfig(BaseModel):
    enabled: Optional[bool] = None
    ustirovka_enabled: Optional[bool] = None
    default_calibration: Optional[bool] = None
    pilot_power_setting: int = 70
    ref_freq: int = 10_450_000
